package com.lms.dao.apply;

import com.lms.bean.UserApply;
import com.lms.dao.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

public class ApplyDaoImpl implements ApplyDao {
    private PreparedStatement pstm;
    private ResultSet rs;
    @Override
    public List<UserApply> getUserApplyList(Connection connection, String userCode) throws SQLException {
        List<UserApply> userApplyList=new ArrayList<>();

        if (connection != null) {
            String sql = "select * from user_apply where userCode=? order by apply_creationDate DESC ,applyID DESC";
            Object[] params = {userCode};
            rs = BaseDao.execute(connection, pstm, rs, sql, params);
            while (rs.next()) {
                UserApply userApply = new UserApply();
                userApply.setApplyID(rs.getInt("applyID"));
                userApply.setUserCode(rs.getString("userCode"));
                userApply.setLabCode(rs.getString("labCode"));
                userApply.setApply_applyCount(rs.getInt("apply_applyCount"));
                userApply.setApply_reason(rs.getString("apply_reason"));
                userApply.setApply_time_week(rs.getString("apply_time_week"));
                userApply.setApply_time_interval(rs.getString("apply_time_interval"));
                userApply.setApply_state(rs.getInt("apply_state"));
                userApply.setApply_creationDate(rs.getDate("apply_creationDate"));
                userApply.setApply_approvalDate(rs.getDate("apply_approvalDate"));
                userApply.setApply_rootId(rs.getString("apply_rootId"));
                userApplyList.add(userApply);
            }
            BaseDao.closeResource(connection, pstm, rs);
        }
        return userApplyList;

    }

    @Override
    public int apply(Connection connection, String userCode, String labCode, String applyCount, String reason, String week, String interval) throws SQLException {
        int flag=0;
        if (connection!=null){
            String sql="INSERT INTO user_apply (userCode, labCode, apply_applyCount, apply_reason, apply_time_week, apply_time_interval," +
                    " apply_state,apply_creationDate)" +
                    " VALUES (?, ?, ?, ?, ?, ?, 3, ?);";
            Object[] params={userCode,labCode,applyCount,reason,week,interval,new SimpleDateFormat("yyyy-MM-dd").format(new Date())};
            flag=BaseDao.execute(connection,pstm,sql,params);
            BaseDao.closeResource(connection,pstm,rs);
        }
        return flag;
    }

    @Override
    public int cancelApply(Connection connection, String applyID) throws SQLException {
        int flag=0;
        if (connection!=null){
            String sql="update user_apply set apply_state=4 where applyID=?";
            Object[] params={applyID};
            flag=BaseDao.execute(connection,pstm,sql,params);
            BaseDao.closeResource(connection,pstm,rs);
        }
        return flag;
    }

    @Override
    public int getApplyCount(Connection connection) throws SQLException {
        int flag=0;
        if (connection!=null){
            String sql="select count(*) as applyCount from user_apply where apply_state=3";
            Object[] params={};
            rs=BaseDao.execute(connection,pstm,rs,sql,params);
            while (rs.next()){
                flag=rs.getInt("applyCount");
            }
            BaseDao.closeResource(connection,pstm,rs);
        }
        return flag;
    }
    @Override
    public int getCheckCount(Connection connection, String userCode) throws SQLException {
        if (connection!=null){
            String sql="select COUNT(*) as checkCount from user_apply where userCode=? and apply_check=0 and apply_state in (1,2);";
            rs = BaseDao.execute(connection,pstm,this.rs,sql,new Object[]{userCode});
            while (rs.next()){
                return rs.getInt("checkCount");
            }
        }
        return 0;
    }

    @Override
    public boolean check(Connection connection, String userCode) throws SQLException {
        if (connection!=null){
            String sql="update user_apply set apply_check=1 where userCode=? and apply_state in (1,2)";
            Object[] params={userCode};
            if (BaseDao.execute(connection,pstm,sql,params)>0) {
                BaseDao.closeResource(connection, pstm, rs);
                return true;
            }
            else {
                BaseDao.closeResource(connection, pstm, rs);
                return false;
            }
        }
        BaseDao.closeResource(connection, pstm, rs);
        return false;
    }

    @Override
    public Map getApplyMsg(Connection connection, String applyID) throws SQLException {
        Map<String, String> map = new HashMap<>();
        if (connection!=null){
            String sql="SELECT openID,labName,apply_applyCount,apply_time_week,apply_time_interval FROM user_account AS a,user_apply as u,lab_data as l where a.userCode=u.userCode and u.labCode=l.labCode and applyID=? ";
            Object[] params={applyID};
            rs=BaseDao.execute(connection,pstm,rs,sql,params);
            while (rs.next()){
                map.put("openID",rs.getString("openID"));
                map.put("labName",rs.getString("labName"));
                map.put("applyCount",rs.getString("apply_applyCount"));
                String time="";
                switch (rs.getString("apply_time_week")){
                    case "mon":time+="周一";break;
                    case "tue":time+="周二";break;
                    case "wed":time+="周三";break;
                    case "thur":time+="周四";break;
                    case "fri":time+="周五";break;
                    case "sat":time+="周六";break;
                    case "sun":time+="周天";break;
                }
                switch (rs.getString("apply_time_interval")){
                    case "one":time+="1-2";break;
                    case "two":time+="3-4";break;
                    case "three":time+="5-6";break;
                    case "four":time+="7-8";break;
                    case "five":time+="9-10";break;
                }
                map.put("applyTime",time);
            }
        }
        return map;
    }
}
